package com.game.base.view.dialog;

import android.content.Context;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 弹框提示
 * Created by Su on 2019/5/24.
 */
public class DialogUtils {

    // ========================= Tips ========================
    private static QMUITipDialog tipDialog;

    /**
     * loading
     */
    public static void showLoadingDialog(Context context, String tips) {
        tipDialog = new QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(tips)
                .create();
        tipDialog.show();
    }

    /**
     * 成功
     */
    public static void showSuccessDialog(Context context, String tips) {
        tipDialog = new QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord(tips)
                .create();
        tipDialog.show();
        closeTipDialogDelay();
    }

    /**
     * 失败
     */
    public static void showFailDialog(Context context, String tips) {
        tipDialog = new QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord(tips)
                .create();
        tipDialog.show();
        closeTipDialogDelay();
    }


    public static void closeTipDialogDelay() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                closeTipDialog();
                this.cancel();
            }
        }, 1500);
    }

    public static void closeTipDialog() {
        if (tipDialog != null) {
            tipDialog.dismiss();
        }
        tipDialog = null;
    }


    // ========================= 对话框 ========================

    /**
     * 消息类型对话框
     */
    public static void showMessagePositiveDialog(Context context, String title, String content, QMUIDialogAction.ActionListener listener) {
        showMessagePositiveDialog(context, title, content, "确定", listener);
    }

    public static void showMessagePositiveDialog(Context context, String title, String content, String okStr, QMUIDialogAction.ActionListener listener) {
        new QMUIDialog.MessageDialogBuilder(context)
                .setTitle(title)
                .setMessage(content)
                .addAction("取消", (dialog, index) -> dialog.dismiss())
                .addAction(okStr, (dialog, index) -> {
                    dialog.dismiss();
                    if (listener != null) {
                        listener.onClick(dialog, index);
                    }
                })
                .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();
    }
}
